app.controller('UserOneCtrl', ['$scope', '$timeout', '$http', '$compile', 'toaster', '$location', '$modal', 'R', 'FileUploader', '$q', '$rootScope',
function($scope, $timeout, $http, $compile, toaster, $location, $modal, R, FileUploader, $q, $rootScope) {
    
    R.getAddressList().then(function(data){
        $scope.mappers.address = data.bizData;

        var urlparams = $location.search();
        $scope.id = urlparams.id;

        if ($scope.id) {
        	$scope.item = $rootScope.getSgObj('USERONE');
        	if (!$scope.item){
                $location.url("/app/user/list");
        	}

        	if ($scope.item.countryId){
        		angular.forEach($scope.mappers.address, function(item){
                    if (item.id == $scope.item.countryId){
                        $scope.model.l1 = item;
                    
                        if ($scope.item.provinceId){
                            angular.forEach(item.childs, function(item){
                                if (item.id == $scope.item.provinceId){
                                    $scope.model.l2 = item;

                                    if ($scope.item.cityId){
                                        angular.forEach(item.childs, function(item){
                                            if (item.id == $scope.item.cityId){
                                                $scope.model.l3 = item;
                                            }
                                        });
                                    }
                                }
                            });
                        }
                    }
                });
        	}
        }else{
            $scope.item = {
                account: '',
                icon: '',
                pwd: '',
                role: 0,
                companyName: '',
                phone: '',
                countryId: null,
                provinceId: null,
                cityId: null
            }
        }
    });

    $scope.$watch('model.l1', function(newval){
    	if (newval && $scope.model.l2.parentId != newval.id){
        	$scope.model.l2 = null;
        }
    });

    $scope.$watch('model.l2', function(newval){
        if (newval && $scope.model.l3.parentId != newval.id){
        	$scope.model.l3 = null;
        }
    });

    $scope.mappers = {
        role: [{
            'id': 0,
            'name': '普通用户'
        },{
            'id': 1,
            'name': '运营人员'
        },{
            'id': 2,
            'name': '管理员'
        }]
    }

    $scope.model = {
        l1: null,
        l2: null,
        l3: null
    }

    $scope.uploader = new FileUploader({
        url: R.getFileUpload(),
        formData: [{
            type: 2
        }],
        autoUpload: true,
        alias: 'inputFile'
    });

    $scope.uploader.onCompleteItem = function(res, data){
        $scope.item.icon = data.bizData.attachmentUrl;
        $scope.item.attachmentUrl = data.bizData.attachmentUrl;
        $scope.item.attachmentName = data.bizData.attachmentName;
        $scope.item.attachmentSize = data.bizData.attachmentSize;
    };


    $scope.opened = [];
    $scope.open = function($event,index) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened[index] = true;
    };

    $scope.submit = function(){
        if ($scope.model.l1) {
        	$scope.item.countryId = $scope.model.l1.id;
        }
        if ($scope.model.l2) {
        	$scope.item.provinceId = $scope.model.l2.id;
        }
        if ($scope.model.l3) {
        	$scope.item.cityId = $scope.model.l3.id;
        }
        if ($scope.item.pwd){
            $scope.item.pwd = md5($scope.item.pwd);
        }

        if ($scope.item.id){
            R.updateUser($scope.item).then(function(data){
                toaster.success("操作成功");
                $location.url("/app/user/list");
            });
        }else{
			var user = $rootScope.getSgObj('LOGINUSER');
          
            $scope.item.userId = user.id;

            R.addUser($scope.item).then(function(data){
                toaster.success("操作成功");
                $location.url("/app/user/list");
            });
        }
    }

}]);